#!/bin/bash

rm -rf ~/MissionPlanner-latest &>/dev/null
mkdir ~/MissionPlanner-latest
cd ~/MissionPlanner-latest
wget https://firmware.ardupilot.org/Tools/MissionPlanner/MissionPlanner-latest.zip || error "Failed to download MissionPlanner-latest.zip!"
unzip MissionPlanner-latest.zip || error "Failed to extract MissionPlanner-latest.zip!"
rm -f MissionPlanner-latest.zip
cd

#fix dependency issue on Bullseye
package_installed mono-complete && sudo apt-mark auto mono-complete

#determine if getting the mono repository is is necessary
status -n "Is the mono-complete package new enough? "

if package_is_new_enough mono-complete 6.8.0 ;then
  status_green 'Yes' #answer the question asked above
else
  status_green 'No' #answer the question asked above
  echo "Adding Mono repository..."
  install_packages apt-transport-https dirmngr gnupg ca-certificates || exit 1
  sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF || error "Failed to add a key to the repos!"
  echo "deb https://download.mono-project.com/repo/debian stable-raspbianbuster main" | sudo tee /etc/apt/sources.list.d/mono-official-stable.list
fi
install_packages mono-complete || exit 1

echo "[Desktop Entry]
StartupNotify=true
Terminal=false
Type=Application
Name=Mission Planner
Exec=bash -c 'LIBGL_ALWAYS_SOFTWARE=1 mono $HOME/MissionPlanner-latest/MissionPlanner.exe'
Icon=$(dirname "$0")/icon-64.png
Categories=Development;Electronics;
Comment=Command and control autonomous planes, vehicles and boats" > ~/.local/share/applications/mission-planner.desktop

